﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using zjx.Wpf.Graffitiboard.Adorners;
using zjx.Wpf.Graffitiboard.Core;

namespace zjx.Wpf.Graffitiboard.Controls
{
    public class GraffitiStroke : Control, ISelectable
    {
        private VisualCollection visualChildren;
        InkPresenter ip = new InkPresenter();

        #region 属性
        #region IsSelected
        private bool isSelected = false;

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }

            set
            {
                this.isSelected = value;
            }
        }
        #endregion

        #region ID
        private Guid id = Guid.Empty;

        public Guid ID
        {
            get
            {
                return this.id;
            }
        }
        #endregion

        #region SelectionAdorner
        private ResizeRotateAdorner selectionAdorner;

        public ResizeRotateAdorner SelectionAdorner
        {
            get
            {
                return this.selectionAdorner;
            }

            set
            {
                this.selectionAdorner = value;
            }
        }
        #endregion
        #endregion

        #region 构造函数
        public GraffitiStroke()
        {
            visualChildren = new VisualCollection(this);
            visualChildren.Add(this.ip);
        }

        public GraffitiStroke(Stroke stylusPoints) : this()
        {
            ip.Strokes.Add(stylusPoints);
        }
        #endregion

        // Override the VisualChildrenCount and 
        // GetVisualChild properties to interface with 
        // the adorner's visual collection.
        protected override int VisualChildrenCount
        {
            get { return visualChildren.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            return visualChildren[index];
        }

        #region 公用方法
        #region GetBounds
        public Rect GetBounds()
        {
            Rect itemRect = VisualTreeHelper.GetDescendantBounds(this);
            Rect itemBounds = this.TransformToAncestor((UIElement)VisualTreeHelper.GetParent(this)).TransformBounds(itemRect);
            return itemBounds;
        }
        #endregion

        #region MoveTo
        /// <summary>
        /// 移动到指定位置
        /// </summary>
        /// <param name="offsetX">水平偏移量</param>
        /// <param name="offsetY">垂直偏移量</param>
        public void MoveTo(double offsetX, double offsetY)
        {
            Point ptChange = this.RenderTransform.Transform(new Point(offsetX, offsetY));
            double left = InkCanvas.GetLeft(this);
            double top = InkCanvas.GetTop(this);

            if (double.IsNaN(left)) left = 0;
            if (double.IsNaN(top)) top = 0;

            InkCanvas.SetLeft(this, left + ptChange.X);
            InkCanvas.SetTop(this, top + ptChange.Y);
        }
        #endregion
        #endregion
    }
}
